/**
 * @ author: 汪孟苹
 */
define(['jquery','bbcare'],function($,$bb){
	var servers = {};
	var URL = $bb.baseUrl("followapp");
	var flag; // 问卷中是否配置了"table_relation"题型

	/**
	 * 自我检查记录 / 查询患者所有任务
	 */
	servers.ajaxUserCpTasks = function(params,callback){
		$bb.ajax({
			url: URL + "ctrl/selfManage/qryUserCpTaskList",
			data:JSON.stringify({
				data : {
					"roleType":"1",
					"busiTypeList":params.busiTypeList,
					"subBusiTypeList":params.subBusiTypeList,
					"userId":params.info.userId,
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询患者自我检查记录出错！");
				}
			}
		});
	}

	/**
	 * 自我检查记录 / 查询患者任务问卷及答案
	 */
	servers.ajaxSelfRecordTempl = function(params,callback,index){
		$bb.ajax({
			url: URL + "ctrl/selfManage/qrySelfRecordTempl",
			data:JSON.stringify({
				data : {
					"templId":params.templId,
					"taskId":params.id,
					"userId":params.userId,
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,params,index);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询患者自我检查记录出错！");
				}
			}
		});
	}

	/**
	 * 处理table数据
	 */
	servers.handleTable = function(data,values){
		flag = false; // 默认无"table_relation"题型
		for (var i = 0; i < data.quesList.length; i++) {
			data.quesList[i].templData = handleTbodyData(data.quesList[i].templData,data.valueList,data.quesList[i].templData.tmplName,values);
		}
		return {
			"data":data,
			"flag":flag
		};
	}

	/**
	 * table里的value
	 */
	var getValue = function(values,index,day){
		if(values && $.isArray(values) && values.length > 0){
			for (var i = 0; i < values.length; i++) {
				if( Number(values[i].day) == (day-1) ){
					return values[i].valueList[index].quesValue;
				}
			}
		}
	}
   
    /**
	 * 处理表格tbody数据
	 */
	var handleTbodyData = function(list,valueList,preName,values){
		var arr = [];
		for(var i = 0; i < list.length; i++){
			arr[i] = list[i];
			if( arr[i].type == "table_relation" && flag == false ) flag = true;
			if( arr[i].type == "table_relation" ){
				var arr1 = arr[i].thead;
				var arr2 = arr[i].tbody;
				arr[i].newtbody = [];
				arr[i].newthead = [];
				for (var j = 1; j < arr1.length; j++) {
					arr[i].newtbody[j-1] = [];
					arr[i].newtbody[j-1][0] = {};
					arr[i].newtbody[j-1][0].text = arr1[j];
					arr[i].newtbody[j-1][1] = {};
					arr[i].newtbody[j-1][1].text = getValue(values,j-1,1);
					arr[i].newtbody[j-1][2] = {};
					arr[i].newtbody[j-1][2].text = getValue(values,j-1,2);
					arr[i].newtbody[j-1][3] = {};
					arr[i].newtbody[j-1][3].text = getValue(values,j-1,3);
					arr[i].newtbody[j-1][4] = {};
					arr[i].newtbody[j-1][4].text = getValue(values,j-1,4);
					arr[i].newtbody[j-1][5] = {};
					arr[i].newtbody[j-1][5].text = getValue(values,j-1,5);
					arr[i].newtbody[j-1][6] = {};
					arr[i].newtbody[j-1][6].text = getValue(values,j-1,6);
					arr[i].newtbody[j-1][7] = {};
					arr[i].newtbody[j-1][7].text = getValue(values,j-1,7);
				}
				arr[i].newthead[0] = arr[i].tbody[0][0].text;
				for (var j = 0; j < arr2.length; j++) {
					for (var p = 0; p < arr2[j].length; p++) {
						arr[i].newthead[j+1] = arr2[j][p].text;
					}
				}
			}else if( arr[i].type == "group" ){
				arr[i].children = handleTbodyData(arr[i].children,valueList,arr[i].label);
			}else{
				if( !arr[i].label ) arr[i].label = preName;
				for (var j = 0; j < valueList.length; j++) {
					if( valueList[j].deCode == arr[i].deCode ){
						var value;
						switch (arr[i].type) {
							case "checkbox":
							case "checkbox_relation":
								value = JSON.parse(valueList[j].quesValue).join("、");
								break;
							case "gestation":
								value = parseInt(valueList[j].quesValue / 7) + "周" + (valueList[j].quesValue % 7) + "天";
								break;
							case "radio":
								value = valueList[j].quesValue;
								for (var p = 0; p < arr[i].list.length; p++) {
									if( arr[i].list[p].bgColor && arr[i].list[p].key == value ){
										arr[i].bgColor = arr[i].list[p].bgColor;
									}
								}
								break;
							default:
								value = valueList[j].quesValue;
								break;
						}
						arr[i].quesValue = value;
					}
				}	
			}
		}
		return arr;
	}

	/**
	 * 微信用户建档绑定模块 / 根据传入paramCode 查询问卷（新生儿早期记录1-7天问卷查询）
	 */
	servers.ajaxChildcareConData = function(params,callback,datas){
		$bb.ajax({
			url: URL + "webapi/service/usertask/qryChildcareConData",
			data:JSON.stringify({
				data : {
					"userId":params.userId,
					"paramCode":"B0104017",
				}
			}),
			success: function(data){
				if(data.success){
					callback(data.data,datas);
				}else{
					$bb.toastShow(data.msg ? data.msg : "查询新生儿早期记录1-7天问卷出错！");
				}
			}
		});
	}


	return servers;
});